<?php
/*
 * [JINYUN!] (C)2001-2099 Jinyunweb.com
 * This is NOT a freeware, use is subject to license terms
 *退款
*/
defined('BY_JYA') or exit('error');
$_RQ['op']=$_RQ['op']?$_RQ['op']:'list';
$tabs=array(
	array('op'=>'list','title'=>'待退款'),
	array('op'=>'list1','title'=>'已退款'),
	array('op'=>'list2','title'=>'已拒绝'),
);
if($_RQ['op']=='list'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=0;
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['realname'])){
		$mid=pdo_getcolumn('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'realname like %'=>$_RQ['realname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['title'])){
		$pid=pdo_getcolumn('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['title'].'%'),'id');
		$params['pid']=$pid;
		$return['params']['pid']=$pid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','pid','reason','payid','money','type','status','createtime','overtime');
	$list=pdo_getslice('jy_zhongchou_withdraw',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$project=pdo_get('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']));
			$item['nickname']=$member['nickname'];
			$item['realname']=$member['realname'];
			$item['avatar']=$member['avatar'];
			$item['title']=$project['title'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'realname','title'=>'申请人姓名','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'money','title'=>'退款金额','type'=>'image'),
			array('name'=>'type','title'=>'退款类型','type'=>'radio','options'=>'type'),
			array('name'=>'reason','title'=>'原因','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'string'),
		),
		'action' => 
		    array (
			       array('type'=>'post','op'=>'ok','title'=>'退款','message'=>'确定退款吗！'),
				   array('type'=>'post','op'=>'no','title'=>'拒绝','message'=>'确定拒绝吗！'),
				   ),),
		'search'=>array(
			array('name'=>'nickname','width'=>15,'title'=>'昵称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'真实姓名','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'项目名称','type'=>'string'),
		)
	);
	//$return['type']=array('0'=>'');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list1'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=1;
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['realname'])){
		$mid=pdo_getcolumn('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'realname like %'=>$_RQ['realname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['title'])){
		$pid=pdo_getcolumn('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['title'].'%'),'id');
		$params['pid']=$pid;
		$return['params']['pid']=$pid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','pid','reason','payid','money','type','status','createtime','overtime');
	$list=pdo_getslice('jy_zhongchou_withdraw',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$project=pdo_get('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']));
			$item['nickname']=$member['nickname'];
			$item['realname']=$member['realname'];
			$item['avatar']=$member['avatar'];
			$item['title']=$project['title'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'realname','title'=>'申请人姓名','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'money','title'=>'退款金额','type'=>'image'),
			array('name'=>'type','title'=>'退款类型','type'=>'radio','options'=>'type'),
			array('name'=>'reason','title'=>'原因','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'string'),
			array('name'=>'overtime','title'=>'处理时间','type'=>'string'),
		),
		'action' => 
		    array (
			     //  array('type'=>'post','op'=>'ok','title'=>'退款','message'=>'确定退款吗！'),
				//  array('type'=>'post','op'=>'no','title'=>'拒绝','message'=>'确定拒绝吗！'),
				   ),),
		'search'=>array(
			array('name'=>'nickname','width'=>15,'title'=>'昵称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'真实姓名','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'项目名称','type'=>'string'),
		)
	);
	//$return['type']=array('0'=>'');
	$return['htmls']=$htmls;
	exi($return);
}
if($_RQ['op']=='list2'){
	$params=array();
	$params['uniacid']=$_SESSION['uniacid'];
	$params['status']=2;
	if(!empty($_RQ['nickname'])){
		$mid=pdo_getcolumn('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'nickname like %'=>$_RQ['nickname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['realname'])){
		$mid=pdo_getcolumn('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'realname like %'=>$_RQ['realname'].'%'),'mid');
		$params['mid']=$mid;
		$return['params']['mid']=$mid;
	}
	if(!empty($_RQ['title'])){
		$pid=pdo_getcolumn('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'title like %'=>$_RQ['title'].'%'),'id');
		$params['pid']=$pid;
		$return['params']['pid']=$pid;
	}
	$pindex = max(1, intval($_RQ['page']));
	$psize = 10;
	$total='';
	$fields=array('id','uniacid','mid','pid','reason','payid','money','type','status','createtime','overtime');
	$list=pdo_getslice('jy_zhongchou_withdraw',$params,array($pindex,$psize),$total,$fields,'','id desc');
	if($list){
		foreach($list as &$item){
			$member=pdo_get('jy_zhongchou_member',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$item['mid']));
			$project=pdo_get('jy_zhongchou_project',array('uniacid'=>$_SESSION['uniacid'],'id'=>$item['pid']));
			$item['nickname']=$member['nickname'];
			$item['realname']=$member['realname'];
			$item['avatar']=$member['avatar'];
			$item['title']=$project['title'];
		}
	}
	$return['list']=$list;
	$totalpage=ceil($total/$psize);
	$totalpage=intval($totalpage);
	$return['totalpage'] =$totalpage;
	$return['params']['page'] =$pindex;
	$htmls=array(
		'type'=>'list',
		'tabs'=>$tabs,
		'list'=>array('data'=>'list','edit'=>true,'list'=>array(
			array('name'=>'title','title'=>'项目名称','type'=>'string'),
			array('name'=>'realname','title'=>'申请人姓名','type'=>'string'),
			array('name'=>'nickname','title'=>'昵称','type'=>'string'),
			array('name'=>'avatar','title'=>'头像','type'=>'image'),
			array('name'=>'money','title'=>'退款金额','type'=>'image'),
			array('name'=>'type','title'=>'退款类型','type'=>'radio','options'=>'type'),
			array('name'=>'reason','title'=>'原因','type'=>'string'),
			array('name'=>'createtime','title'=>'申请时间','type'=>'string'),
				array('name'=>'overtime','title'=>'处理时间','type'=>'string'),
		),
		'action' => 
		    array (
			       //array('type'=>'post','op'=>'ok','title'=>'退款','message'=>'确定退款吗！'),
				   //array('type'=>'post','op'=>'no','title'=>'拒绝','message'=>'确定拒绝吗！'),
				   ),),
		'search'=>array(
			array('name'=>'nickname','width'=>15,'title'=>'昵称','type'=>'string'),
			array('name'=>'realname','width'=>15,'title'=>'真实姓名','type'=>'string'),
			array('name'=>'title','width'=>15,'title'=>'项目名称','type'=>'string'),
		)
	);
	//$return['type']=array('0'=>'');
	$return['htmls']=$htmls;
	exi($return);
}

if($_RQ['op']=='del'){
	$del=trim($_RQ['del'],';');
	if($del){
		$del=explode(';',$del);
	}else{
		$del=array();
	}
	if($_RQ['id']){
		$del[]=$_RQ['id'];
	}
	foreach($del as $id){
		$id=intval($id);
		if($id){
			pdo_delete('jy_zhongchou_withdraw',array('id'=>$id,'uniacid'=>$_SESSION['uniacid']));
		}
	}
	exi('删除成功！');
}
if($_RQ['op']=='no'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要拒绝的退款申请！','error');
	}
	pdo_update('jy_zhongchou_withdraw',array('status'=>2,'overtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('已拒绝该退款申请！');
}
if($_RQ['op']=='ok'){
	$id=intval($_RQ['id']);
	if(!$id){
		exi('请先选择要退款的退款申请！','error');
	}
	$withdraw=pdo_get('jy_zhongchou_withdraw',array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	$fans=pdo_get('core_fans',array('uniacid'=>$_SESSION['uniacid'],'mid'=>$withdraw['mid']));
	$basic=$this->plugin_setting('basic');
	
	$arr['openid'] = $fans['openid'];
	$arr['mchid'] = $basic['mchid'];
	$arr['mch_appid'] = $basic['mch_appid'];
	$arr['amount'] = $withdraw['money'];
	$res =$this->tixian($arr);
	if($res['result_code'] == 'SUCCESS'){
		pdo_update('jy_zhongchou_withdraw',array('status'=>1,'overtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
		exi('提现成功，金额已转入到对方钱包！');
		
	}else{
		exi('提现失败：'.$res['result_msg'],'error');
	}
	pdo_update('jy_zhongchou_withdraw',array('status'=>1,'overtime'=>TIMESTAMP),array('uniacid'=>$_SESSION['uniacid'],'id'=>$id));
	exi('退款成功');
}

function tixian($arr) {
		global $_AR,$_RQ;
		$data['mch_appid'] = $arr['mch_appid'];
		$data['mchid'] = $arr['mchid'];
		$data['nonce_str'] = createNoncestr();
		$data['openid'] = $arr['openid'];
	    $data['nick_name'] = $arr['hbname'];
	    $data['check_name'] = 'FORCE_CHECK';
	    $data['partner_trade_no'] = $arr['partner_trade_no'];	
		//$data['send_name'] = $_SESSION['account']['title'];
		$data['amount'] = $arr['amount']*100;
		$data['spbill_create_ip'] = $_SERVER['REMOTE_ADDR'];
		//$data['act_name'] = $arr['hbname'];
		$data['desc'] = '提现';
		if(!$data['openid']) 
		{
			$rearr['return_msg']='缺少用户openid';
			return $rearr;
		}
		$data['sign'] =getSign($data);
		
		$xml = arrayToXml($data);
		$url ="https://api.mch.weixin.qq.com/mmpaymkttransfers/promotion/transfers";
		$re = wxHttpsRequestPem($xml,$url);
		$rearr = xmlToArray($re);
		//exi($re,'error');
		return $rearr;
}
	function createNoncestr( $length = 32 ) {
		$chars = "abcdefghijklmnopqrstuvwxyz0123456789";
		$str ="";
		for ( $i = 0; $i < $length; $i++ ) 
		{
			$str.= substr($chars, mt_rand(0, strlen($chars)-1), 1);
		}
		return $str;
	}
	function getSign($Obj) {
		foreach ($Obj as $k => $v) 
		{
			$Parameters[$k] = $v;
		}
		ksort($Parameters);
		$String = formatBizQueryParaMap($Parameters, false);
		$setting = uni_setting();
		$String = $String."&key=".$setting['pay']['wechat_miyue'];
	//	exi($setting['pay']['wechat_miyue'],'error');
		$String = md5($String);
		$result_ = strtoupper($String);
		return $result_;
	}
	function formatBizQueryParaMap($paraMap, $urlencode) {
		$buff = "";
		ksort($paraMap);
		foreach ($paraMap as $k => $v) 
		{
			if($urlencode) 
			{
				$v = urlencode($v);
			}
			$buff .= $k . "=" . $v . "&";
		}
		$reqPar;
		if (strlen($buff) > 0) 
		{
			$reqPar = substr($buff, 0, strlen($buff)-1);
		}
		return $reqPar;
	}
	function arrayToXml($arr) {
		$xml = "<xml>";
		foreach ($arr as $key=>$val) 
		{
			if (is_numeric($val)) 
			{
				$xml.="<".$key.">".$val."</".$key.">";
			}
			else $xml.="<".$key."><![CDATA[".$val."]]></".$key.">";
		}
		$xml.="</xml>";
		return $xml;
	}
	function wxHttpsRequestPem( $vars,$url, $second=30,$aHeader=array()) {
		//global $_W;
		$ch = curl_init();
		
		curl_setopt($ch,CURLOPT_TIMEOUT,$second);
		curl_setopt($ch,CURLOPT_RETURNTRANSFER, 1);
		curl_setopt($ch,CURLOPT_URL,$url);
		curl_setopt($ch,CURLOPT_SSL_VERIFYPEER,false);
		curl_setopt($ch,CURLOPT_SSL_VERIFYHOST,false);
		
		curl_setopt($ch,CURLOPT_SSLCERTTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLCERT,getcwd().'/addons/jy_h5game/cert/'.$_SESSION['uniacid'].'/apiclient_cert.pem');
		curl_setopt($ch,CURLOPT_SSLKEYTYPE,'PEM');
		curl_setopt($ch,CURLOPT_SSLKEY,getcwd().'/addons/jy_h5game/cert/'.$_SESSION['uniacid'].'/apiclient_key.pem');
	
	
	
		if( count($aHeader) >= 1 )
		{
			curl_setopt($ch, CURLOPT_HTTPHEADER, $aHeader);
		}
		curl_setopt($ch,CURLOPT_POST, 1);
		curl_setopt($ch,CURLOPT_POSTFIELDS,$vars);
		$data = curl_exec($ch);
		//exi($data,'error');
		if($data)
		{
			curl_close($ch);
			return $data;
		}
		else 
		{
			$error = curl_errno($ch);
			echo "call faild, errorCode:$error\n";
			curl_close($ch);
			return false;
		}
	} 
	function xmlToArray($xml) {
		$array_data = json_decode(json_encode(simplexml_load_string($xml, 'SimpleXMLElement', LIBXML_NOCDATA)), true);
		return $array_data;
	}